import unittest
import os
import ddt
from common.data_operation import DataOperation
from common.functions import browser_operation
from page.reception.index_page import IndexPage
from page.reception.register_page import RegisterPage
# TODO 测试正常数据流程,仅必填数据
# 准备测试数据
path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
file_path = os.path.join(path, "data/register.csv")
register_list = DataOperation(file_path).get_data_to_dict()


@ddt.ddt
class RegisterTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        """
        打开浏览器，创建对象
        """
        driver = browser_operation()
        cls.reg = RegisterPage(driver)
        cls.index = IndexPage(driver)

    @ddt.data(*register_list)
    def test_register(self, data):
        """
        注册主流程测试
        """
        # 请求网址
        self.reg.get_website(self.reg.register_url)
        # 输入用户名
        self.reg.username_ipt(data['username'])
        # 输入邮箱
        self.reg.email_ipt(data['email'])
        # 输入密码
        self.reg.password1_ipt(data['password'])
        # 确认密码
        self.reg.password2_ipt(data['password'])
        # 输入手机号
        self.reg.phone_tel_ipt(data['telphone'])
        # 点击立即注册
        self.reg.register_button()
        # 断言
        username = self.index.font_text()
        self.assertEqual(data['username'], username)

    def tearDown(self) -> None:
        """
        注册后退出登录
        """
        self.index.logout()

    @classmethod
    def tearDownClass(cls) -> None:
        """
        关闭浏览器
        """
        cls.reg.quit()


if __name__ == '__main__':
    unittest.main(verbosity=2)
